/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package jsf.generator.pages;

import net.db.generator.field.models.BaseFieldModel;
import net.db.generator.utils.ClassGeneratorUtil;

/**
 *
 * @author abosancic
 */
public class JSFListWriter extends JSFBaseWriter{

    public JSFListWriter() {
        fileName = "List";
    }

    @Override
    protected void generateDefineTitle() {
        stringXHTML.append(spaces).append("<ui:define name=\"title\">\n")
                .append(spaces).append(spaces).append("<h:outputText value=\"#{bundle.List" + ClassGeneratorUtil.humanize(className) + "Title}\"></h:outputText>\n")
                .append(spaces).append("</ui:define>").append("\n");
        properties.put("List" + ClassGeneratorUtil.humanize(className) + "Title", "List");
    }
    
    @Override
    protected void generateFields() {
        
        stringXHTML.append(spaces).append("<h:outputText escape=\"false\" value=\"#{bundle.List" + ClassGeneratorUtil.humanize(className) + "Empty}\" rendered=\"#{" + ClassGeneratorUtil.humanizeToField(className) + "Controller.items.rowCount == 0}\"/>\n");
        properties.put("List" + ClassGeneratorUtil.humanize(className) + "Empty", "(No " + ClassGeneratorUtil.humanize(className) + " Items Found)");
        
        generateGroupPanel();
        generateStartTable();
        
        for (BaseFieldModel fieldModel : fields) {
            String field = fieldModel.getFieldName();
            field = ClassGeneratorUtil.humanizeToField(fieldModel.getFieldName());
            stringXHTML.append(spaces).append(spaces).append("<h:column>\n")
                    .append(spaces).append(spaces).append(spaces).append("<f:facet name=\"header\">\n")
                    .append(spaces).append(spaces).append(spaces).append(spaces).append("<h:outputText value=\"#{bundle.List" + ClassGeneratorUtil.humanize(className) + "Title_" + field + "}\"/>\n")
                    .append(spaces).append(spaces).append(spaces).append("</f:facet>\n")
                    .append(spaces).append(spaces).append(spaces).append(spaces).append("<h:outputText value=\"#{item." + field + "}\"/>\n")
                    .append(spaces).append(spaces).append("</h:column>")
                    .append("\n");
            
            // add Bundle
            properties.put("List" + ClassGeneratorUtil.humanize(className) + "Title_" + field, ClassGeneratorUtil.humanize(field));
        }
        
        generateEditColumn();
        generateEndTable();
        generateEndPanel();
        
        stringXHTML.append(spaces).append("\n");
    }
    
    @Override
     protected void generateEndForm() {
        stringXHTML.append(spaces).append(" </h:panelGroup>\n")
                .append(spaces).append("<br />\n")
                .append(spaces).append("<h:commandLink action=\"#{" + ClassGeneratorUtil.humanizeToField(className) + "Controller.destroyAndView}\" "
                + "value=\"#{bundle.List" + ClassGeneratorUtil.humanize(className) + "DestroyLink}\" />\n")
                .append(spaces).append(spaces).append("<br />\n")
                .append(spaces).append(spaces).append("<br />\n")
                .append(spaces).append("<h:link outcome=\"Edit\" value=\"#{bundle.List" + ClassGeneratorUtil.humanize(className) + "EditLink}\"/>\n")
                .append(spaces).append("<br />\n")
                .append(spaces).append("<h:commandLink action=\"#{" + ClassGeneratorUtil.humanizeToField(className) + "Controller.prepareCreate}\" value=\"#{bundle.View" + ClassGeneratorUtil.humanize(className) + "CreateLink}\" immediate=\"true\"/>")
                .append(spaces).append("<br />\n")
                .append(spaces).append("<h:commandLink action=\"#{" + ClassGeneratorUtil.humanizeToField(className) + "Controller.prepareList}\" value=\"#{bundle.View" + ClassGeneratorUtil.humanize(className) + "ShowAllLink}\" immediate=\"true\"/>")
                .append(spaces).append("<br />\n")
                .append(spaces).append("<h:link outcome=\"/index\" value=\"#{bundle.List" + ClassGeneratorUtil.humanize(className) + "IndexLink}\" />\n")
                .append(spaces).append("</h:form>\n");
        
    }

    private void generateGroupPanel() {
        String name = ClassGeneratorUtil.humanizeToField(className);
         stringXHTML.append(spaces).append("<h:panelGroup rendered=\"#{" + name + "Controller.items.rowCount > 0}\">\n")
                .append(spaces).append("<h:outputText class=\"badge\" value=\"#{" + name + "Controller.pagination.pageFirstItem + 1}..#{" + name + "Controller.pagination.pageLastItem + 1}/#{" + name + "Controller.pagination.itemsCount}\"/>&nbsp;")
//                .append(spaces).append("<h:commandLink action=\"#{" + name + "Controller.previous}\" value=\"#{bundle.Previous} #{" + name + "Controller.pagination.pageSize}\" rendered=\"#{" + name + "Controller.pagination.hasPreviousPage}\"/>&nbsp;")
//                    .append(spaces).append("<h:commandLink action=\"#{" + name + "Controller.next}\" value=\"#{bundle.Next} #{" + name + "Controller.pagination.pageSize}\" rendered=\"#{" + name + "Controller.pagination.hasNextPage}\"/>&nbsp;")
//                    .append(spaces).append("<br />\n");
         
                .append(spaces).append("<h:commandLink value=\"#{bundle.Previous} #{" + name + "Controller.pagination.pageSize}\" rendered=\"#{" + name + "Controller.pagination.hasPreviousPage}\">\n")
                .append(spaces).append("<f:ajax listener=\"#{" + name + "Controller.previousAjax}\" render=\"output dataPanel\" />\n")
                .append(spaces).append("</h:commandLink> &nbsp;\n")
                .append(spaces).append("<h:commandLink value=\"#{bundle.Next} #{" + name + "Controller.pagination.pageSize}\" rendered=\"#{" + name + "Controller.pagination.hasNextPage}\">\n")
                .append(spaces).append("<f:ajax listener=\"#{" + name + "Controller.nextAjax}\" render=\"output dataPanel\" />\n")
                .append(spaces).append("</h:commandLink> &nbsp;\n")
                .append(spaces).append("<br />\n");
         
         
        properties.put("Previous", "Previous");
        properties.put("Next", "Next");
        properties.put("PersistenceErrorOccured", "A persistence error occurred.");
    }

    private void generateStartTable() {
        stringXHTML.append(spaces).append("<h:dataTable class=\"table table-bordered\"  id=\"output\" value=\"#{" + ClassGeneratorUtil.humanizeToField(className) + "Controller.items}\" var=\"item\" border=\"0\" cellpadding=\"2\" cellspacing=\"0\" rowClasses=\"jsfcrud_odd_row,jsfcrud_even_row\" rules=\"all\" style=\"border:solid 1px\">\n")
                .append(spaces).append("\n");
    }

    private void generateEndTable() {
        stringXHTML.append(spaces).append(spaces).append("</h:dataTable>\n");
    }

    private void generateEditColumn() {
        stringXHTML.append(spaces).append(spaces).append("<h:column>\n")
                    .append(spaces).append(spaces).append(spaces).append("<f:facet name=\"header\">\n")
                    .append(spaces).append(spaces).append(spaces).append(spaces).append("<h:outputText value=\"&nbsp;\"/>\n")
                    .append(spaces).append(spaces).append(spaces).append("</f:facet>\n")
                    .append(spaces).append(spaces).append(spaces).append(spaces).append("<h:commandLink action=\"#{" + ClassGeneratorUtil.humanizeToField(className) + "Controller.prepareView}\" value=\"#{bundle.List" + ClassGeneratorUtil.humanize(className) + "ViewLink}\"/>\n")
                    .append(spaces).append(spaces).append("<h:outputText value=\" \"/>\n")   
                    .append(spaces).append(spaces).append(spaces).append(spaces).append("<h:commandLink action=\"#{" + ClassGeneratorUtil.humanizeToField(className) + "Controller.prepareEdit}\" value=\"#{bundle.List" + ClassGeneratorUtil.humanize(className) + "EditLink}\"/>\n")
                    .append(spaces).append(spaces).append("<h:outputText value=\" \"/>\n")   
                    .append(spaces).append(spaces).append(spaces).append(spaces).append("<h:commandLink action=\"#{" + ClassGeneratorUtil.humanizeToField(className) + "Controller.destroy}\" value=\"#{bundle.List" + ClassGeneratorUtil.humanize(className) + "DestroyLink}\"/>\n")
                    .append(spaces).append(spaces).append("<h:outputText value=\" \"/>\n")   
                    .append(spaces).append(spaces).append("</h:column>")
                    .append("\n");                        
    }
    
    private void generateEndPanel() {
        stringXHTML.append(spaces).append("</h:panelGroup>\n");
//                .append(spaces).append("<br />\n")
//                .append(spaces).append("<h:commandLink action=\"#{" + ClassGeneratorUtil.humanizeToField(className) + "Controller.prepareCreate}\" value=\"#{bundle.List" + ClassGeneratorUtil.humanize(className) + "CreateLink}\"/>\n")
//                .append(spaces).append("<br />\n")
//                .append(spaces).append("<br />\n")
//                .append(spaces).append("<h:commandLink action=\"#{" + ClassGeneratorUtil.humanizeToField(className) + "Controller.prepareCreate}\" value=\"#{bundle.List" + ClassGeneratorUtil.humanize(className) + "CreateLink}\"/>\n")
//                .append(spaces).append("<br />\n")
//                .append(spaces).append("<br />\n")
//                .append(spaces).append("<h:link outcome=\"/index\" value=\"#{bundle.List" + ClassGeneratorUtil.humanize(className) + "IndexLink}\"/>\n")
//                .append(spaces).append("</h:form>\n");
                
    }
    
    @Override
    protected void addInitProperties() {
        String name = ClassGeneratorUtil.humanize(className);
        properties.put("List" + name + "DestroyLink", "Destroy");
        properties.put("List" + name + "EditLink", "Edit");
        properties.put("List" + name + "CreateLink", "Create New " + name);
        properties.put("List" + name + "ViewLink", "View");
        properties.put("List" + name + "IndexLink", "Index");
    }
}
